package com.nexgo.payment.action.inputauthcode;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.action.Action;
import com.nexgo.payment.action.ActionResult;
import com.nexgo.payment.action.ActionStep;
import com.nexgo.payment.action.OnActionListener;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.ui.BaseFragment;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.widget.TitleBuilder;
import com.nexgo.payment.widget.keyboard.NumKeyBoard;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 输入授权码界面
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年8月24日
 */
public class InputAuthCodeFragment extends BaseFragment implements Action<ActionStep>, InputAuthCodeContract.View, BaseFragment.DownTimerListener {

    @BindView(R.id.et_auth_code)
    EditText etAuthCode;
    @BindView(R.id.key_board)
    NumKeyBoard keyBoard;
    @BindView(R.id.title_tv_right)
    TextView tvTitleRight;
    Unbinder unbinder;

    private OnActionListener mListener;
    private InputAuthCodeContract.Presenter mPresenter;

    public InputAuthCodeFragment() {
        // Required empty public constructor
    }

    public static InputAuthCodeFragment newInstance() {
        InputAuthCodeFragment fragment = new InputAuthCodeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new InputAuthCodePresenter(this, Repository.getInstance());
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_input_auth_code, container, false);
        unbinder = ButterKnife.bind(this, view);

        initTitle(view);

        return view;
    }

    //初始化标题栏
    private void initTitle(View view) {
        new TitleBuilder(view)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(R.string.input_auth_code_title))
                .build();
    }

    @Override
    protected void initEvent() {
        keyBoard.setCallback(keyType -> mPresenter.pressKey(keyType));
        etAuthCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPresenter.onTextChange(s);
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }

    @Override
    protected void initData() {
        //启动倒计时
        setDownTimer(SysConstant.TIME_OUT_60, this);
    }

    @OnClick({R.id.title_iv_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_iv_left:
                if (mListener != null) {
                    mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, null));
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnActionListener) {
            mListener = (OnActionListener) context;
        }
    }

    @Override
    public ActionStep getActionStep() {
        return ActionStep.INPUT_AUTH_CODE;
    }

    @Override
    public void setPresenter(InputAuthCodeContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showInputResult(String result) {
        etAuthCode.setText(result);
        etAuthCode.setSelection(etAuthCode.getText().length());
    }

    @Override
    public void showCancel() {
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, null));
        }
    }

    @Override
    public void showConfirm(String authCode) {
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.SUCC, authCode));
        }
    }

    @Override
    public void showErrFormat() {
        restartDownTimer();
        ToastUtils.showToast(getString(R.string.err_input));
    }

    @Override
    public void showEmpty() {
        ToastUtils.showToast(getString(R.string.input_can_no_be_empty));
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        cancelDownTimer();
        unbinder.unbind();
    }

    @Override
    public void onTick(long secondsUntilFinished) {
        if (!isActive()) return;
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText(String.valueOf(secondsUntilFinished));
    }

    @Override
    public void onFinish() {
        if (isActive()) tvTitleRight.setText("0");
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_TIMEOUT, getString(R.string.input_auth_code_time_out)));
        }
    }
}
